package com.template.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.entity.Department;
import com.template.entity.Staff;
import com.template.entity.User;
import com.template.service.DepartmentService;
import com.template.service.StaffService;
import com.template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用于跳转List页面的controller类
 */
@Controller
public class ListController {


    @Autowired
    UserService userService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    StaffService staffService;

    @GetMapping("/userList")
    public String userListPage(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                               @RequestParam(value = "departmentid", required = false) Integer departmentid,
                               @RequestParam(value = "keyword", required = false) String keyword,
                               HttpServletRequest request,
                               Model model) {
        //  获取登录信息
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (departmentid == null) {
            departmentid = loginUser.getDepartmentid();
            //  System.out.println(departmentid);
        }
        session.setAttribute("departmentid", departmentid);

        Page<User> page = userService.selectAllPageByWrapper(pn, departmentid, keyword);
        List<User> records = page.getRecords();
        List<Department> departmentList = departmentService.list();

        for (User record : records) {
            Department department = departmentService.getById(record.getDepartmentid());
            record.setDepartmentname(department.getName());
        }
        page.setRecords(records);

        model.addAttribute("page", page);
        model.addAttribute("departmentList", departmentList);

        return "user/userList";
    }

    @GetMapping("/staffList")
    public String staffListPage(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                                @RequestParam(value = "departmentid", required = false) Integer departmentid,
                                @RequestParam(value = "keyword", required = false) String keyword,
                                HttpServletRequest request,
                                Model model) {
        //  获取登录信息
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (departmentid == null) {
            departmentid = loginUser.getDepartmentid();
        }
        session.setAttribute("departmentid", departmentid);

        Page<Staff> page = staffService.selectAllPageByWrapper(pn, departmentid, keyword);
        List<Staff> records = page.getRecords();
        List<Department> departmentList = departmentService.list();

        for (Staff record : records) {
            Department department = departmentService.getById(record.getDepartmentid());
            record.setDepartmentname(department.getName());
        }
        page.setRecords(records);

        model.addAttribute("page", page);
        model.addAttribute("departmentList", departmentList);

        return "staff/staffList";
    }

    @GetMapping("/departmentList")
    public String departmentList(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        Page<Department> page = departmentService.selectAllPage(pn);

        model.addAttribute("page", page);

        return "department/departmentList";
    }
}
